package com.app.sdnotepad;

import java.io.File;
import java.util.Arrays;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NPDirMain extends ListActivity {
	
	// Menu item ids
    public static final int MENU_ITEM_NEW = Menu.FIRST;
    public static final int MENU_ITEM_EXIT = Menu.FIRST + 1;
    String[] noteList;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		/* check default folder, and read file list */
		File hardCodeDir = new File("/sdcard/app/note"); 
		if (hardCodeDir.exists() == false)
		{
			hardCodeDir.mkdirs();
		}
		noteList = hardCodeDir.list();
//		File file1 = new File("/sdcard/app/note/123.txt"); 
//		try {
//			boolean createNewFile = file1.createNewFile();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		super.onCreate(savedInstanceState);
		Arrays.sort(noteList);    // java.util.Arrays.sort(noteList);
		
		ArrayAdapter<String> noteAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, noteList);
		
		setListAdapter(noteAdapter);
		
		
		/* listactivity has a default layout that consists of a full-screen list int he center */
		/* no need to set another content view, if possible */
		//setContentView(R.layout.dirmain);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		// TODO Auto-generated method stub
		
		menu.add(0, MENU_ITEM_NEW, 0, R.string.CmdNewFile);
		menu.add(0, MENU_ITEM_EXIT, 0, R.string.CmdExit);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId())
		{
			case MENU_ITEM_NEW:
			{
				String cmdString = "CMD_NEW_FILE";
				Intent startEditViewIntent = new Intent();
				startEditViewIntent.setClass(this, NPEditMain.class);
				startEditViewIntent.putExtra("CMD", cmdString);
				startActivity(startEditViewIntent);
				this.finish();
				break;
			}
			case MENU_ITEM_EXIT:
			{
				
				break;
			}
			default:
			{
				break;
			}
		}
		
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		
//		String cmdString = "CMD_OPEN_FILE";
//		Intent startEditViewIntent = new Intent();
//		startEditViewIntent.setClass(this, NPEditMain.class);
//		startEditViewIntent.putExtra("CMD", cmdString);
//		startActivity(startEditViewIntent);
//		this.finish();
//		
		
		String fileName = null;
		Intent startEditViewIntent = new Intent();
		startEditViewIntent.setClass(this, NPEditMain.class);
		String cmdString = "CMD_OPEN_FILE";
		
		super.onListItemClick(l, v, position, id);
		
		fileName = getListView().getAdapter().getItem(position).toString(); // get file name
		
		/* create intent, and fill value */
		
		startEditViewIntent.putExtra("CMD", cmdString);
		startEditViewIntent.putExtra("FILENAME", fileName);
		startActivity(startEditViewIntent);
		this.finish();
		
	}
}
